﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

public partial class giohangcuaban : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataConnection.str = @"Data Source=xe;User ID=ODER;Password=12345";
        if (!IsPostBack)
        {
            if (Session["TenDN"] != null)
            {
                Loaddulieu();
            }
            else
            {
                MultiView1.ActiveViewIndex = 1;
            }
        }

    }
    private void Loaddulieu()
    {
        MultiView1.ActiveViewIndex = 0;
        GridView1.PageSize = 5;
        DataTable dt = (DataTable)Session["GioHang"];
        if (dt == null)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
            return;


        }
        else if (dt.Rows.Count > 0)
        {
            Panel1.Visible = false;
            Panel2.Visible = true;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            tinhtien(dt);
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        tinhtien(dt);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["soluong"].ToString() == "0")
                delete(i);

        }
    }
    private void tinhtien(DataTable dt)
    {
        double tien = 0;
        if (Session["GioHang"] != null)
        {
            DataTable dt1 = (DataTable)Session["GioHang"];
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                tien = tien + int.Parse(dt.Rows[i]["dongia"].ToString()) * int.Parse(dt.Rows[i]["soluong"].ToString());

            }
            lbtongtien.Text = "Tổng giá trị đơn hàng: " + tien.ToString();
        }
        else
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
    }
     protected void btnmuatiep_Click(object sender, EventArgs e)
    {
        Response.Redirect("sanpham.aspx");
    }
    
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Loaddulieu();
    }


    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Loaddulieu();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox soLuong = GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox;
        int row = e.RowIndex;
        DataTable dt = (DataTable)Session["GioHang"];
        dt.Rows[row][4] = int.Parse(soLuong.Text);
        Session["GioHang"] = dt;
        GridView1.EditIndex = -1;
     //   Loaddulieu();
        Response.Redirect("GioHangcuaban.aspx");
        
       
     
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Loaddulieu();
    }
    void delete(int row)
    {
        DataTable dt = (DataTable)Session["GioHang"];
        dt.Rows[row].Delete();
        Session["GioHang"] = dt;
        
        Response.Redirect("GioHang.aspx");
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int row = e.RowIndex;
        DataTable dt = (DataTable)Session["GioHang"];
        dt.Rows[row].Delete();
        Session["GioHang"] = dt;
        GridView1.EditIndex = -1;
       
        Response.Redirect("giohangcuaban.aspx");
    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        Loaddulieu();
    }
    protected void btnmuatiep_Click1(object sender, EventArgs e)
    {
        Response.Redirect("sanpham.aspx");
    }
    protected void btnguidon_Click(object sender, EventArgs e)
    {
        Response.Redirect("thanhtoan.aspx");
    }
    protected void btnxoatoanbo_Click(object sender, EventArgs e)
    {
        DataTable dt = (DataTable)Session["GioHang"];
        dt.Rows.Clear();
        Session["GioHang"] = dt;
        Response.Redirect("giohangcuaban.aspx");
    }

}